<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="relesof">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select
        v-model="form.manager"
        style="width:80%"
        placeholder="请选择"
      >
        <el-option
          v-for="item in employees"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />

      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments, getDepartDetail } from '@/api/departments'
export default {
  props: {
    curId: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义code校验规则
    const valicode = (rule, value, callback) => {
      // value,当前表单值
      // callback:回调函数
      // 将code编码帅选出来然后进行判断验证
      let exit = this.originList.map(item => item.code)
      // 如果是编辑模态框,那就要将当前编码从信息中移除,避免出现提示
      if (this.isEdit) {
        // 就是将当前点击的部门列表的id和所有的部门id比较,
        // 将不等的id的数组数据挑出来进行重组
        exit = this.originList.filter(item => item.id !== this.curId).map(item => item.code)
      }
      console.log(exit, '编码')
      // 判断
      if (exit.includes(value)) {
        callback(new Error('部门编码' + value + '已存在'))
      } else {
        callback()
      }
    }
    // 自定义name校验规则
    const valiname = (rule, value, callback) => {
      let exis = this.originList.filter(item => item.pid === this.curId).map(item => item.name)
      if (this.isEdit) {
        // 1.找到当前编辑部门的对象   find->返回第一个找到的对象
        const curD = this.originList.find(item => item.id === this.curId)
        // 2.获取当前编辑对象的pid
        const pid = curD.pid
        // 3.构建数据列表,找到所有兄弟部门和自己
        exis = this.originList.filter(item => item.pid === pid && item.id !== this.curId).map(item => item.name)
      }

      exis.includes(value) ? callback(new Error('部门名称' + value + '已存在')) : callback()
    }
    return {
      // 自定义校验规则

      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      employees: [],
      relesof: {
        name:
         [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
           { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
           { validator: valiname, trigger: 'blur' }
         ],
        code:
         [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
           { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
           { validator: valicode, trigger: 'blur' }
         ],
        manager:
         [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        ntroduce:
         [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
           { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }]
      }

    }
  },
  //   方法三:侦听器
  //   watch: {
  //     curId: {
  //       handler: function(newval, oldval) {
  //         this.loadAdd()
  //       }
  //     //   immediate: true
  //     }
  //   },
  created() {
    this.loadEmployeeSimple()
    if (this.isEdit) {
      this.loadAdd()
    }
  },
  methods: {
    // 获得员工名字信息
    async loadEmployeeSimple() {
      try {
        const res = await getEmployeeSimple()
        this.employees = res.data
        console.log(res, '员工简单信息')
      } catch (error) {
        console.log(error)
      }
    },
    // 新增子部门的ajax
    async loadDepartments() {
      try {
        // 此时还缺少pid,所以要添加pid
        const res = await addDepartments({ pid: this.curId, ...this.form })
        console.log(res, '新增子部门')
        // 关闭弹层和重新渲染
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取编辑子部门的信息的ajax
    async loadAdd() {
      try {
        const res = await getDepartDetail(this.curId)
        console.log(res, '修改内容信息')
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 修改子部门的ajax
    async doEdit() {
      try {
        const res = await updateDepartments(this.form)
        // 关闭弹层和重新渲染
        this.$emit('success')
        console.log(res, '修改部门请求')
      } catch (error) {
        console.log(error)
      }
    },
    // 确定弹框是否是新增还是编辑
    hSubmit() {
      // 兜底校验
      this.$refs.deptForm.validate(valid => {
        if (!valid) return // 校验失败
        this.isEdit ? this.doEdit() : this.loadDepartments()
      })
    },

    // 取消
    hCancel() {
      this.$emit('close')
    },
    // reset
    resetrom() {
      // 重置表单
      this.$refs.deptForm.resetFields()
    }
  }

}
</script>

<style>

</style>
